﻿using System;
using System.Text.RegularExpressions;

/*
 *  author: able
 *  @2012/2/24 17:31:10
 * 
 */
namespace __ts.Core.Util
{
    /// <summary>
    /// 
    /// </summary>
    public static partial class StringExtension
    {
        /// <summary>
        /// 判断大字符串中是否存在制定子串。
        /// 在数据量大时，比IndexOf高效
        /// </summary>
        /// <param name="text"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool HasStr(this string text, string str)
        {
            int textLen = text.Length;
            int offset = str.Length;
            if (textLen < offset)
                return false;

            long badHashcode = 0;
            long prefWindowHashcode = 0;

            for (int i = 0; i < offset; i++)
            {
                badHashcode += (char)str[i];
                prefWindowHashcode += (char)text[i];
            }

            int l = 0;
            int innerloop = (textLen - offset) + 1;
            do
            {
                if (badHashcode == prefWindowHashcode)
                {
                    unsafe
                    {
                        fixed (char* pChar = str.ToCharArray(0x00, offset))
                        {
                            for (int k = 0; k < offset; k++)
                                if (text[l + k] != pChar[k]) break;
                        }
                        return true;
                    }
                }
                if (textLen > (offset + l))
                    prefWindowHashcode -= (char)text[l] - (char)text[l + offset];
            } while ((l++ < innerloop));
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static string GetMatch(this string str, string pattern)
        {
            Match mh = Regex.Match(str, pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            if (mh.Success)
            {
                return mh.Groups[1].Value;
            }
            return string.Empty;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="pattern"></param>
        /// <param name="newValue"></param>
        /// <returns></returns>
        public static string ReplaceMatch(this string str, string pattern, string newValue)
        {
            str = Regex.Replace(str, pattern, new MatchEvaluator(mh =>
            {
                if (mh.Success)
                {
                    return newValue;
                }
                return mh.Value;
            }));
            return string.Empty;
        }

        public static int ToInt32(this string source)
        {
            return source.ToInt32(0);
        }

        public static int ToInt32(this string source, int defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            int value;

            if (!int.TryParse(source, out value))
                value = defaultValue;

            return value;
        }

        public static long ToLong(this string source)
        {
            return source.ToLong(0);
        }

        public static long ToLong(this string source, long defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            long value;

            if (!long.TryParse(source, out value))
                value = defaultValue;

            return value;
        }

        public static short ToShort(this string source)
        {
            return source.ToShort(0);
        }

        public static short ToShort(this string source, short defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            short value;

            if (!short.TryParse(source, out value))
                value = defaultValue;

            return value;
        }

        public static decimal ToDecimal(this string source)
        {
            return source.ToDecimal(0);
        }

        public static decimal ToDecimal(this string source, decimal defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            decimal value;

            if (!decimal.TryParse(source, out value))
                value = defaultValue;

            return value;
        }

        public static DateTime ToDateTime(this string source)
        {
            return source.ToDateTime(DateTime.MinValue);
        }

        public static DateTime ToDateTime(this string source, DateTime defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            DateTime value;

            if (!DateTime.TryParse(source, out value))
                value = defaultValue;

            return value;
        }

        public static bool ToBoolean(this string source)
        {
            return source.ToBoolean(false);
        }

        public static bool ToBoolean(this string source, bool defaultValue)
        {
            if (string.IsNullOrEmpty(source))
                return defaultValue;

            bool value;

            if (!bool.TryParse(source, out value))
                value = defaultValue;

            return value;
        }
    }
}
